<?php
/**
 * Created by IntelliJ IDEA.
 * User: HXL
 * Date: 2018/5/9
 * Time: 13:34
 */
namespace app\order\controller;

use think\Lang;

/**
 * 基表型号
 * Class MeterType
 * @package app\order\controller
 */
class MeterType extends Base
{
    public function index(){
        $this->authVerify();
        $field = 'meter_id,meter_type,caliber,create_time';
        $where = '';
        $order = '';
        $meterTypes = $this->meterTypes()->page($field, $where, $order);
        $this->page($meterTypes);
        $this->assign('meterTypes', $meterTypes);
        return $this->fetch("met/index");
    }

    /**
     * 添加action
     */
    public function save(){
        $this->authVerify();
        $post = input('post.');
        //因为分类是可以编辑的，需要先判断该类在数据库中是否已存在
        $caliber = trim($post['caliber']);
        $meterType = trim($post['meter_type']);
        $meterTypes = array(
            'meter_type'=>$meterType,
            'caliber'=>$caliber,
            'create_time'=>date('Y-m-d H:i:s', time())
        );
        $validate = $this->validate($meterTypes, 'Met');
        //var_dump($meterTypes);exit();
        if(true !== $validate){
            return $this->error(" $validate ");
        }
        $result = $this->meterTypes()->add($meterTypes, '');
        if(!$result){
            return $this->error(Lang::get('add fail'));
        }
        return $this->success(Lang::get('add success'), 'MeterType/index');
    }

    /**
     * json数据
     */
    public function one(){
        $meterId = input('param.meter_id');
        $field = 'meter_id,meter_type,caliber,create_time';
        $where = array('meter_id'=>$meterId);
        $data = $this->meterTypes()->select($field, $where);
        //var_dump($data);
        echo json_encode($data);
    }

    /**
     * 更新action
     */
    public function esave(){
        $this->authVerify();
        $meterId = input('param.meter_id');
        $where = array('meter_id'=>$meterId);
        $find = $this->meterTypes()->findById($where);
        if(!$find){
            return $this->error(Lang::get('unfind metertype'));
        }
        $meterTypes = input('post.');
        //因为分类是可以编辑的，需要先判断该类在数据库中是否已存在
        $meterType = trim($meterTypes['meter_type']);
        $caliber = $meterTypes['caliber'];
        $meterTypes = array(
            'meter_type'=>$meterType,
            'caliber'=>$caliber
        );
        //基表型号是否重复
        /*$newWhere = array('meter_id'=>['<>', $meterId],'meter_type'=>$meterType);
        $isExisted = $this->meterTypes()->findById($newWhere);
        if($isExisted){
            return $this->error(Lang::get('existed metertype'));
        }*/
        $result = $this->meterTypes()->update($meterTypes, $where);
        if($result < 1){
            return $this->error(Lang::get('edit fail'));
        }
        return $this->success(Lang::get('edit success'), 'MeterType/index');
    }


    /**
     * 删除action
     */
    public function del(){
        $this->authVerify();
        $meterId = input('param.meter_id');
        //var_dump($meterId);exit();
        $where = array('meter_id'=>$meterId);
        $find = $this->meterTypes()->findById($where);
        if(!$find){
            return $this->error(Lang::get('unfind metertype'));
        }
        $result = $this->meterTypes()->del($where);
        if($result < 1){
            return $this->error(Lang::get('del fail'));
        }
        return $this->success(Lang::get('del success'), 'MeterType/index');
    }

    /**
     * 简单的模糊搜索
     * @return mixed
     */
    public function search(){
        $search = input('param.search');
        $data = $this->meterTypes()->searchLike($search);
        $this->page($data);
        $this->assign('meterTypes', $data);
        return $this->fetch('met/index');
    }



}